package com.lkd.service.impl;
import com.lkd.entity.SkuEntity;
import java.time.LocalDateTime;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.common.VMSystem;
import com.lkd.dao.VendingMachineDao;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.NodeEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.entity.VmTypeEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.*;
import com.lkd.utils.UUIDUtils;
import com.lkd.vo.Pager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineDao, VendingMachineEntity> implements VendingMachineService {

    @Autowired
    private VmTypeService vmTypeService;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private   ChannelService channelService;
    @Override
    public Long selectCountByNodeId(Long id) {
        return baseMapper.selectCountByNodeId(id);
    }

    @Override
    public Pager<VendingMachineEntity> search(Integer pageIndex, Integer pageSize, Integer status, String innerCode) {
        // 构建分页条件
        Page<VendingMachineEntity> page = new Page<>(pageIndex,pageSize);

       // 构建查询条件
        LambdaQueryWrapper<VendingMachineEntity> qw = Wrappers.lambdaQuery();
        qw.eq(ObjectUtil.isNotNull(status),VendingMachineEntity::getStatus,status);
        qw.eq(StrUtil.isNotBlank(innerCode),VendingMachineEntity::getInnerCode,innerCode);
        qw.orderByDesc(VendingMachineEntity::getCreateTime);
        // 分页查询
        page(page, qw);
        return Pager.build(page);
    }

    @Override
    public Boolean addVm(VendingMachineEntity vendingMachineEntity) {
        VmTypeEntity vmTypeEntity = vmTypeService.getById(vendingMachineEntity.getVmType());
        // 判断售货机类型是否存在
        if(vmTypeEntity == null){
            throw new LogicException("售货机类型不存在");
        }
        NodeEntity nodeEntity = nodeService.getById(vendingMachineEntity.getNodeId());
        // 判断位是否存在
        if(nodeEntity == null){
            throw new LogicException("点位不存在");
        }
        // 新增售货机
        // 生成售货机编码
        String innerCode = UUIDUtils.getUUID();
        vendingMachineEntity.setInnerCode(innerCode);
        vendingMachineEntity.setVmStatus(VMSystem.VM_STATUS_NODEPLOY);   // 售货机状态，0:未投放;1-运营;3-撤机
        vendingMachineEntity.setRegionId(nodeEntity.getRegionId());
        vendingMachineEntity.setOwnerId(nodeEntity.getOwnerId());
        vendingMachineEntity.setBusinessId(nodeEntity.getBusinessId());
        vendingMachineEntity.setOwnerName(nodeEntity.getOwnerName());
        save(vendingMachineEntity);
        // 新增货道数据  几行几列
        Integer row = vmTypeEntity.getVmRow(); //行
        Integer col = vmTypeEntity.getVmCol(); //列

        // 准备一个集合用来收集需要保存的货道对象

        List<ChannelEntity> list = new ArrayList<>();
        for (int r = 1; r <= row; r++) {
            for (int c = 1; c <= col; c++) {
                ChannelEntity channelEntity = new ChannelEntity();
                // channelEntity.setChannelId(0L);
                channelEntity.setChannelCode(r+"-"+c);
                channelEntity.setSkuId(0L);  //代表未放商品
                channelEntity.setVmId(vendingMachineEntity.getId());
                channelEntity.setInnerCode(vendingMachineEntity.getInnerCode());
                channelEntity.setMaxCapacity(vmTypeEntity.getChannelMaxCapacity());
                channelEntity.setPrice(0);
                channelEntity.setCurrentCapacity(0);
                list.add(channelEntity);
            }
        }
       return channelService.saveBatch(list);

    }
}
